home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7974 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.7 KB  |  123 lines

  1. Path: news.th-darmstadt.de!news
  2. From: Enno Sandner <enno@intellektik.informatik.th-darmstadt.de>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Does this create memory leak?...
  5. Date: Fri, 16 Feb 1996 13:16:39 +0100
  6. Organization: Fachbereich Informatik, TH Darmstadt
  7. Message-ID: <312475A7.2781E494@intellektik.informatik.th-darmstadt.de>
  8. References: <3123DF68.1677@sierra.net>
  9. NNTP-Posting-Host: kitz.intellektik.informatik.th-darmstadt.de
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (X11; I; SunOS 4.1.3 sun4m)
  14.  
  15. T Colwell wrote:
  16. > The code which follows seams like it would create a memory leak.  The CAT class contains member pointers to
  17. > objects on the heap.  When you take an existing object at reassign it via an overridden assignment operator to
  18. > a second object of the same type, what happens to the data at the originally pointed-to address?  Here's what I
  19. > mean:
  20. > *****************************************************
  21. >    #include <iostreams.h>
  22. >    class CAT
  23. >    {
  24. >        public:
  25. >             CAT();    // default constructor
  26. >             // copy constructor and destructor intentionally excluded!
  27. >              int GetAge() const { return *itsAge; }
  28. >              int GetWeight() const { return *itsWeight; }
  29. >              void SetAge(int age) { *itsAge = age; }
  30. >              CAT operator=(const CAT &);
  31. >         private:
  32. >              int *itsAge;
  33. >              int *itsWeight;
  34. >    };
  35. >    CAT::CAT()
  36. >    {
  37. >         itsAge = new int;
  38. >         itsWeight = new int;
  39. >         *itsAge = 5;
  40. >         *itsWeight = 9;
  41. >    }
  42. >   CAT CAT::operator=(const CAT & rhs)
  43. >   {
  44. >      if (this == &rhs)
  45. >         return *this;
  46. >         itsAge = new int;
  47. >         itsWeight = new int;
  48. >         *itsAge = rhs.GetAge();
  49. >         *itsWeight = rhs.GetWeight();
  50. >   }
  51. >    void main()
  52. >    {
  53. >         CAT frisky;
  54. >         frisky.SetAge(6);
  55. >         CAT whiskers;
  56. >         whiskers = frisky; // <--doesn't this leave
  57. >                            // the old members of whiskers
  58. >                            // stranded in the heap?
  59. >    }
  60. > **************************************************
  61. > The assignment operator reinitializes itsAge and itsWeight to point to new heap addresses, what happens to the
  62. > addresses they were pointing to before?  Doesn't this create stranded addresses (leaks)?
  63.  
  64. This code will produce a memory leak whenever you create an
  65. instance of class 'CAT'. Usually one needs a copy-ctor, dtor
  66. and assignment-operator if the ctor isn't trivial.
  67. For instance the following code outlines a possible implementation
  68. >>>>
  69. #include <iostream.h>
  70.  
  71. class CAT {
  72. public:
  73.   CAT() : itsAge(new int(5)), itsWeight(new int(9)) {}
  74.   CAT(const CAT& cat) { Copy(cat); }
  75.   ~CAT() { Destroy(); }
  76.   CAT& operator=(const CAT & cat) {
  77.     if (this!=&cat) { Destroy(); Copy(cat); }
  78.     return *this;
  79.   }
  80.   void SetAge(int age) { *itsAge = age; }
  81.  
  82. private:
  83.   int *itsAge;
  84.   int *itsWeight;
  85.  
  86.   void Destroy() { delete itsAge; delete itsWeight; }
  87.   void Copy(const CAT& cat) {
  88.     itsAge=new int(*cat.itsAge); itsWeight=new int(*cat.itsWeight);
  89.   }
  90. };
  91.  
  92. int main() 
  93. {
  94.   CAT frisky;
  95.   frisky.SetAge(6);
  96.   CAT whiskers;
  97.   whiskers = frisky;
  98.  
  99.   return 0;
  100. }
  101. <<<<
  102. Some notes:
  103.      ctor: The member-initializer list is used to initialize the 
  104.            dynamicly maintained integer values.    
  105. copy-ctor: The copy-construction is delegated to the private function 'Copy'.
  106.      dtor: Again a private function is used to release the allocated
  107.            ressources.
  108.    ass-op: The assignment-operator can now be easily expressed in terms
  109.            of destruction ('Destroy') and copy-construction ('Copy').
  110.  
  111. This guarantees that the class has 'proper copy-semantics'.
  112.  
  113.     Enno
  114.